<?php
class Mention {
	private $message = "";
	private $auth = null;
	function Mention() {
		$this->auth = new Authority();
	}

	public function run() {
		$p = '/@mention[\s]+([\S]+)[\s]*([0-9]*)/';
		preg_match($p, trim($_POST[Message]), $m);
		if (!$m) {
			$this->showMention();
			return;
		}

		if (trim($m[1]) == "-1") {
			$this->deleteMention($m);
			return;
		}

		if (trim($m[1]) == "丫丫" && $this->auth->adminLevel < 3) {
			$this->message = "不允许设置主人的名字>_<..";
			return true;
		}

		if ($this->isExists($m[1])) {
			$this->message = "改昵称已经被其他群员使用了。。换一个吧╮(╯▽╰)╭";
		} else {
			if ($m[2] && $this->auth->adminLevel > 3) {
				$this->addMentionByadmin($m);
			} else {
				$this->addMentionBySelf($m);
			}
			return;
		}

	}

	private function addMentionByadmin($m) {
		DB :: SaveTableRow('td_config_alt', array (
			'name' => '@' . $m[1],
			'qq' => $m[2],
			'ClusterNum' => $_POST[ClusterNum],
			'nick' => $_POST[Nick]
		));
		$this->message = "设置成功>_<.";
		return;

	}

	private function addMentionBySelf($m) {
		$sql = "select * from td_config_alt where qq='{$_POST[Sender]}' and ClusterNum='{$_POST[ClusterNum]}'";
		$ret = DB :: GetQueryResult($sql, false);
		if (count($ret) == 0) {
			DB :: SaveTableRow('td_config_alt', array (
				'name' => '@' . $m[1],
				'qq' => $_POST[Sender],
				'ClusterNum' => $_POST[ClusterNum],
				'nick' => $_POST[Nick]
			));
			$this->message = "您已经设置成功了,以后别人只要在群里 @" . $m[1] . " 我就会提示你了>_<~！\n 前提是你是索妮娅的好友~！";
		} else {
			@ DB :: Query("update  td_config_alt set name ='@{$m[1]}'  where qq='{$_POST[Sender]}' and clusternum='{$_POST[ClusterNum]}'");
			$this->message = "您已经将昵称<" . substr($ret[0][name], 1) . ">修改为<{$m[1]}>";
		}

		return;
	}

	private function deleteMention($m) {
		@ DB :: Query("delete from  td_config_alt   where qq='{$_POST[Sender]}' and clusternum='{$_POST[ClusterNum]}'");
		$this->message = "取消@提示成功~！";
		return;
	}

	public function isExists($altName) {

		$ret = @ DB :: GetQueryResult("select * from td_config_alt where name='@{$altName}'  and clusternum='{$_POST[ClusterNum]}'", true);
		if ($ret) {
			return $ret[qq];
		}

		return false;
	}
	private function showMention() {
		$this->message = "使用说明：\n@mention XX (XX代表你要设置的昵称)\n如果提示成功了,那么以后别人在本群@XX 索妮娅就会提示了>_<..";
	}

	public function doMention($altName) {
		if ($qq = $this->isExists($altName)) {
			$p = '/(@[^\s]+)[\s]*(-b)?(-m)?(.*)?/is';
			preg_match($p, trim($_POST[Message]), $m);
			$api = new Api("SendVibration");
			if ($m[2]) {
				$this->message = $api->doApi($qq,"");
			}
			$mess = $_POST[Nick] . "在群[{$_POST[ClusterName]}]({$_POST[ClusterNum]})中@到你了,赶紧去看一下吧！";
			if ($m[3]) {
				if (!$m[4])
					$mess = $_POST[Nick] . "在群[{$_POST[ClusterName]}]({$_POST[ClusterNum]})中@到你了,赶紧去看一下吧！";
				else
					$mess = $_POST[Nick] . "在群[{$_POST[ClusterName]}]({$_POST[ClusterNum]})中@到你了,还说：\n{$m[4]}";

			}
			$api = new Api("SendMessage");
			$this->message =$api->doApi($qq,$mess);

		}

	}
	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}
}
?>